"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from msnp.logger import Logger
from PyQt4 import QtCore
import traceback

def readFromFile(filepath):
    """This function reads the content of the given file.
    
    Args:
        filepath: A String containing the path to the file which content should be read.
        
    Returns:
        The content of the given file is returned.
    """
    try:
        f = open(filepath, 'r')
        s = f.read()
        return s
    except:
        Logger.info("Error while reading from file "+filepath)
        Logger.info(traceback.format_exc())
    finally:
        f.close()
        
def overwriteFile(filepath, content):
    """This function writes the given content into the given file.
    
    Args:
        filepath: A String containing the path to the file which content should be read.
        content: A String containing the content to write into the file.
    """
    try:
        #w-> overwrite
        f = open(filepath,"w")
        f.write(content)
    except:
        Logger.info("Error while overwriting file "+filepath)
        Logger.info(traceback.format_exc())
    finally:
        f.close()
        
def getHomeDirectory():
    """This function retrieves the home directory of the application.
    
    Returns:
        The home directory of the application is returned.
    """
    try:
        return str(QtCore.QDir.homePath())
    except:
        Logger.info("Error while searching home directory")
        Logger.info(traceback.format_exc())
    return None
